import time

import pyautogui
from exceptiongroup import catch
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from head.Init_Driver import SurveyExaminationSystem_Driver


class QuestionBankCenter:
    url = ""
    driver = ""
    def __init__(self):
        # 集成测试
        self.url = "http://8.155.1.153:8081/repo/index"
        self.driver = SurveyExaminationSystem_Driver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()

        # 单元测试
        # self.url = "http://8.155.1.153:8081/user/login"
        # self.driver = SurveyExaminationSystem_Driver.driver
        # self.driver.get(self.url)
        # # 初始化登录成功状态
        # self.driver.find_element(By.ID, "username").send_keys("karen")
        # self.driver.find_element(By.ID, "password").send_keys("karen1234")
        # self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        # 检查是否登录成功
        # assert WebDriverWait(self.driver, 3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//a[h1='问卷系统']")))
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#sk-layout > div > div > section > div > header > div > div.ant-pro-global-header-right-content > div > div > div")))
        # 点击“题库中心”进入相关页面
        # self.driver.find_element(By.XPATH, "//span[text()='题库中心']").click()
        # time.sleep(3)

    def MyQuestionBankUIFuncTest(self):
        # 点击“我的题库”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='我的题库']")))
        self.driver.find_element(By.XPATH,"//li[span='我的题库']").click()
        # 测试点击下拉选项卡框，并选择选项，然后模拟鼠标点击enter
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='请选择']")))
        dropdown = self.driver.find_element(By.ID,"mode")
        dropdown.click()
        dropdown.send_keys(Keys.ENTER)
        # 点击“查询”按钮
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        # time.sleep(2)
        # 点击“重置”按钮
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # time.sleep(2)
        # 测试名称输入框是否支持查询
        assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.ID,"name")))
        self.driver.find_element(By.ID,"name").send_keys("我不存在哦")
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # 点击“新建”按钮，测试是否跳转至相关页面
        self.driver.find_element(By.XPATH,"//button[span='新建']").click()
        assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.ID,"name")))
        time.sleep(1)
        self.driver.refresh()
        # 测试题库列表中“文本导入”按钮是否正常
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.LINK_TEXT,"文本导入")))
        self.driver.find_element(By.LINK_TEXT,"文本导入").click()
        time.sleep(1)
        # 检查是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[span='添加试题']")))
        self.driver.find_element(By.XPATH,"//button[span='关 闭']").click()
        # 测试题库列表中“模版导入”按钮是否正常
        # self.driver.refresh()
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.LINK_TEXT,"模板导入")))
        self.driver.find_element(By.LINK_TEXT,"模板导入").click()
        time.sleep(1)
        # 检查是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[p='点击或者拖拽Excel文件到此处']")))
        self.driver.find_element(By.XPATH,"//div[p='点击或者拖拽Excel文件到此处']").click()
        # 设置安全暂停，每执行一段pyautogui代码时，会暂停一秒，最好别完全依赖它，必要时添加time.sleep()
        pyautogui.PAUSE = 1
        # 启动故障安全功能
        pyautogui.FAILSAFE = True
        # 上传文件，键盘和鼠标前后连续按enter和左键才能成功，单次不可以
        pyautogui.sleep(1) # 在输入文件名称之前最好搞个强制等待，亲测不然会输入乱序
        filepath = "D:\Desktop\TestFile.xlsx"
        pyautogui.write(filepath,interval=0.1)
        # pyautogui.sleep(1)
        pyautogui.press('enter')
        # pyautogui.sleep(1)
        # 获取屏幕分辨率
        print(f'当前屏幕分辨率为：{pyautogui.size()}')
        # 获取鼠标要点击的地方
        print(f'鼠标需要移动到：{pyautogui.position()}')
        # 移动要点击的地方，在一秒内
        pyautogui.moveTo(940,850,duration=0.5)
        # 单击左键
        pyautogui.click(button='left')
        # 点击导入上传
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='导入']")))
        self.driver.find_element(By.XPATH, "//span[text()='导入']").click()
        # time.sleep(3)

    def ProblemManagementUIFuncTest(self):
        # 点击“问题管理”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='问题管理']")))
        self.driver.find_element(By.XPATH,"//li[span='问题管理']").click()
        # 测试类型下拉选项框
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#sk-layout > div > div > section > div > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(1) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > span.ant-select-selection-search")))
        dropdown = self.driver.find_element(By.ID,"mode")
        dropdown.click()
        dropdown.send_keys(Keys.ENTER)
        # 测试点击下拉选项卡框
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#sk-layout > div > div > section > div > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(2) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > span.ant-select-selection-search")))
        dropdown = self.driver.find_element(By.ID,"questionType")
        dropdown.click()
        dropdown.send_keys(Keys.ARROW_DOWN)
        dropdown.send_keys(Keys.ENTER)
        # 点击“查询”按钮
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        time.sleep(1)
        # 点击“重置”按钮
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # 测试试题列表中“编辑”按钮
        try:
            WebDriverWait(self.driver,3).until(
                expected_conditions.visibility_of_element_located(
                    (By.LINK_TEXT,"编辑")))
            self.driver.find_element(By.LINK_TEXT,"编辑").click()
            # 测试是否弹出相关页面，并返回至问题管理页面
            assert WebDriverWait(self.driver,3).until(
                expected_conditions.visibility_of_element_located((By.XPATH,"//button[span='关 闭']")))
            time.sleep(1)
            self.driver.find_element(By.XPATH,"//button[span='关 闭']").click()
        except:
            print('succed')
        # time.sleep(3)

    def MyNotesUIFuncTest(self):
        # 点击“我的笔记”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='我的笔记']")))
        self.driver.find_element(By.XPATH,"//li[span='我的笔记']").click()
        # 测试“问题标题”输入框和“查询”按钮功能是否正常
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='问题标题']")))
        self.driver.find_element(By.ID,"name").send_keys("1==1")
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        time.sleep(1)
        # 点击“重置”按钮
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # 点击“展开”按钮
        self.driver.find_element(By.LINK_TEXT,"展开").click()
        # 选择日期,测试功能是否正常
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='创建时间']")))
        self.driver.find_element(By.CSS_SELECTOR,"div[class='ant-picker-input ant-picker-input-active']").click()
        self.driver.find_element(By.CSS_SELECTOR,"#createAt").send_keys("2025-08-25")
        self.driver.find_element(By.CSS_SELECTOR,"div[class='ant-picker-input']").click()
        Date = self.driver.find_element(By.CSS_SELECTOR,"div[class='ant-picker-input'] input[placeholder='请选择']")
        Date.send_keys("2025-09-01")
        pyautogui.press('enter')
        Date1 = self.driver.find_element(By.CSS_SELECTOR,"div[class='ant-picker-input ant-picker-input-active'] input[placeholder='请选择']")
        Date1.send_keys("2025-09-05")
        # 点击键盘enter
        pyautogui.press('enter')
        # 点击“查询”按钮
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        # 点击“重置”按钮
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # 点击“我的收藏”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='我的收藏']")))
        self.driver.find_element(By.XPATH,"//div[text()='我的收藏']").click()
        # 检测是否成功跳转至相关页面
        try:
            WebDriverWait(self.driver,3).until(
                expected_conditions.visibility_of_element_located(
                    (By.XPATH,"#sk-layout > div > div > section > div > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > ""div > div > div > div > div > table > thead > tr > th:nth-child(2)")))
        except:
            print("succed")


# questionBankCenter = QuestionBankCenter()
# questionBankCenter.MyQuestionBankUIFuncTest()
# questionBankCenter.ProblemManagementUIFuncTest()
# questionBankCenter.MyNotesUIFuncTest()
# questionBankCenter.driver.quit()